package com.dikang.yygh.user.api;


import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dikang.yygh.common.exception.YyghException;
import com.dikang.yygh.common.result.Result;
import com.dikang.yygh.common.result.ResultCodeEnum;
import com.dikang.yygh.common.utils.AuthContextHolder;
import com.dikang.yygh.model.user.Patient;
import com.dikang.yygh.user.service.PatientService;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 就诊人表 前端控制器
 * </p>
 *
 * @author k3
 * @since 2021-08-26
 */
@Api("就诊人管理")
@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {
    @Autowired
    private PatientService patientService;
    // 获取就诊人信息列表
    @GetMapping("auth/findAll")
    public Result getPatientList(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> patients = patientService.getPatientList(userId);

        return Result.ok(patients);


    }
    // 添加就诊人
    @PostMapping("auth/save")
    public Result savePatient(@RequestBody  Patient patient, HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        System.out.println(patient);
        return Result.ok();
    }
    // 根据id获取就诊人信息
    @GetMapping("auth/get/{id}")
    public Result getPatientById(@PathVariable("id") Long id){
        Patient patient = patientService.getPatientById(id);
        return Result.ok(patient);
    }

    // 修改就诊人
    @PostMapping("auth/update")
    public Result updatePatient(@RequestBody Patient patient, HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        boolean b = patientService.updateById(patient);
        return b ? Result.ok():Result.fail();
    }
    // 删除就诊人
    @DeleteMapping("auth/remove/{id}")
    public Result removePatient(@PathVariable("id") Long id){
        return patientService.removeById(id)? Result.ok() : Result.fail();
    }
    @ApiOperation(value = "获取就诊人")
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(
            @ApiParam(name = "id", value = "就诊人id", required = true)
            @PathVariable("id") Long id) {
        return patientService.getById(id);
    }

}

